﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using V5_Common;
using V5_EShop.Data;

namespace V5_EShop.Admin {
    public partial class Admin_Config : AdminPage {
        protected void Page_Load(object sender, EventArgs e) {
            if (!this.IsPostBack) {
                Action = InputHelper.SetString(Request["Action"]).Trim().ToLower();
                switch (Action) {
                    case "1":
                        this.MultiViewList.ActiveViewIndex = 1;
                        break;
                    case "2":
                        this.MultiViewList.ActiveViewIndex = 2;
                        break;
                    case "3":
                        this.MultiViewList.ActiveViewIndex = 3;
                        break;
                    case "4":
                        this.MultiViewList.ActiveViewIndex = 4;
                        break;
                    default:
                        Bind_ConfigEdit();
                        break;
                }
            }
        }

        private void Bind_ConfigEdit() {
            this.MultiViewList.ActiveViewIndex = 0;

            ModelConfig model = new ModelConfig(p => p.ID == 1);
            if (model != null) {
                this.hfID.Value = model.ID.ToString();
                this.txtSiteTitle.Text = model.SiteTitle;
                this.txtSiteUrl.Text = model.SiteUrl;
                this.txtWebmasterName.Text = model.WebmasterName;
                this.txtWebmasterEmail.Text = model.WebmasterEmail;
                this.txtSiteCopyright.Text = model.SiteCopyright;
                this.txtSiteDescription.Text = model.SiteDescription;
                this.txtSiteKeywords.Text = model.SiteKeywords;
                this.rblSiteState.SelectedValue = model.SiteState.ToString();
                this.txtSiteCloseText.Text = model.SiteCloseText;
                this.ddlMailObject.SelectedValue = model.MailObject.ToString();
                this.txtMailServer.Text = model.MailServer;
                this.txtMailServerUserName.Text = model.MailServerUserName;
                this.txtMailServerPassWord.Text = model.MailServerPassWord;
                this.txtMailDomain.Text = model.MailDomain;
                //
                //this.ddlUrlRewrite.SelectedIndex = this.ddlUrlRewrite.Items.IndexOf(this.ddlUrlRewrite.Items.FindByText(model.UrlRewrite));
                //
                this.txtUploadType.Text = model.UploadType;
                this.txtUploadMwidth.Text = model.UploadMwidth;
                this.txtUploadSwidth.Text = model.UploadSwidth;
                //
                this.txtSiteLogo.Text = model.SiteLogo;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e) {
            ModelConfig model = new ModelConfig(p => p.ID == 1);
            int ID = InputHelper.SetNumber(this.hfID.Value);
            //基本配置
            string SiteTitle = this.txtSiteTitle.Text;
            string SiteUrl = this.txtSiteUrl.Text;
            string WebmasterName = this.txtWebmasterName.Text;
            string WebmasterEmail = this.txtWebmasterEmail.Text;
            string SiteCopyright = this.txtSiteCopyright.Text;
            string SiteDescription = this.txtSiteDescription.Text;
            string SiteKeywords = this.txtSiteKeywords.Text;
            int SiteState = int.Parse(this.rblSiteState.SelectedValue);
            string SiteCloseText = this.txtSiteCloseText.Text;
            int MailObject = int.Parse(this.ddlMailObject.SelectedValue);
            string MailServer = this.txtMailServer.Text;
            string MailServerUserName = this.txtMailServerUserName.Text;
            string MailServerPassWord = this.txtMailServerPassWord.Text;
            string MailDomain = this.txtMailDomain.Text;
            //
            string urlrewrite = "aspx";// this.ddlUrlRewrite.SelectedValue;
            string uploadtype = this.txtUploadType.Text;
            string uploadmwidth = this.txtUploadMwidth.Text;
            string uploadswidth = this.txtUploadSwidth.Text;
            //
            string SiteLogo = this.txtSiteLogo.Text;
            if (ID > 0) {
                model.ID = ID;
                model.SiteTitle = SiteTitle;
                model.SiteUrl = SiteUrl;
                model.WebmasterName = WebmasterName;
                model.WebmasterEmail = WebmasterEmail;
                model.SiteCopyright = SiteCopyright;
                model.SiteDescription = SiteDescription;
                model.SiteKeywords = SiteKeywords;
                model.SiteState = SiteState;
                model.SiteCloseText = SiteCloseText;
                model.MailObject = MailObject;
                model.MailServer = MailServer;
                model.MailServerUserName = MailServerUserName;
                model.MailServerPassWord = MailServerPassWord;
                model.MailDomain = MailDomain;
                //
                model.UrlRewrite = urlrewrite;
                model.UploadType = uploadtype;
                model.UploadMwidth = uploadmwidth;
                model.UploadSwidth = uploadswidth;
                //
                model.SiteLogo = SiteLogo;

                model.Update();

                //V5.Config.Clear();
                Response.Redirect(HttpsHelper.ComeUrl);
            }
        }
    }
}
